var widgeon = Components.classes['@berjon.com/widgeon;1'].getService().wrappedJSObject;
      var u = widgeon.utils;
      u.addWarnHandler(function (str) { document.getElementById("list").innerHTML += str + "<br/>"; });
      // u.addWarnHandler(function (str) { document.getElementById("log").value += str + "\n"; });
      u.addErrorHandler(function (str) { alert("ERROR: " + str); });

      function init () {
          try {
              updateList();
          }
          catch (e) {
              dump("ERROR: " + e + "\n");
          }
      }
      
      // XXX this is all very rudimentary at the moment
      function updateList2 () {
          var list = widgeon.getWidgetList();
          var el = document.getElementById("list");
          el.textContent = "";
          for (var i = 0; i < list.length; i++) {
              var w = list[i];
              var cfg = w.config();
              var uuid = w.uuid;
              var name = "Unnamed Widget";
              if (cfg.shortName) name = cfg.shortName;
              if (cfg.name) name = cfg.name;
              var icon = "XXX-FAIL";
              if (cfg.icons && cfg.icons.length) icon = "widget://" + uuid + "/" + cfg.icons[0].src;
              el.innerHTML += "<div class='widget'><table><tr><td rowspan='2'><img src='" +
                              icon + "' width='50' height='50'/></td><td><h3>" +
                              name + "</h3></td></tr><tr><td class='meta'><a onclick='run(\"" +
                              uuid + "\")'>run</a> • <a onclick='remove(\"" +
                              uuid + "\")'>remove</a></td></tr></table></div>";
          }
      }
      
      function updateList () {
          var list = widgeon.getWidgetList();
          var el = document.getElementById("icon-box");
          while (el.lastChild) {
            el.lastChild.removeEventListener("click", onClick, false);
            el.removeChild(el.lastChild);
          }
          for (var i = 0; i < list.length; i++) {
              var w = list[i];
              var cfg = w.config();
              var uuid = w.uuid;
              var name = "Unnamed Widget";
              if (cfg.shortName) name = cfg.shortName;
              if (cfg.name) name = cfg.name;
              var icon = "chrome://widgeon/skin/default_icon.png";
              if (cfg.icons && cfg.icons.length) icon = "widget://" + uuid + "/" + cfg.icons[0].src;
              let image = document.createElement("image");
              image.setAttribute("class", "widgeon-launcher");
              image.setAttribute("src", icon);
              image.setAttribute("width", "32");
              image.setAttribute("height", "32");
              image.setAttribute("uuid", uuid);
              image.setAttribute("context", "context");
              image.setAttribute("tooltiptext", name);
              image.addEventListener("click", onClick, false);
              el.appendChild(image);
          }
          window.sizeToContent();
      }
      
      function onClick(event) {
        let uuid = event.target.getAttribute("uuid");
        if (event.button == 0) {
            run(uuid);
        }
        else {
            document.getElementById("widgeon-remove-menu").uuid = uuid;
        }
      }
      
      function run (uuid) {
          widgeon.createWidget(uuid).open();
      }

      function remove (event) {
          var res = confirm("Do you really want to remove this widget? ");
          if (res) {
              widgeon.removeWidget(event.target.uuid);
              updateList();
          }
      }
  
      function browse () {
          var nsIFilePicker = Components.interfaces.nsIFilePicker;
          var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
          fp.init(window, "Select a widget", nsIFilePicker.modeOpen);
          fp.appendFilter("Widget Packages", "*.wgt");
          fp.appendFilter("Generic Packages", "*.zip; *.jar");
          fp.appendFilters(nsIFilePicker.filterAll);
          var res = fp.show();
          if (res == nsIFilePicker.returnOK){
              var file = fp.file;
              var widget = widgeon.installWidget(file);
              updateList();
              widget.open();
          }
      }